/*
 * @(#)EmptyResponseInterceptor.java
 *
 * Copyright 2011 Xinhua Online, Inc. All rights reserved.
 */

package com.winxuan.channel.support.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.winxuan.channel.model.object.ChannelResult;


/**
 * description
 * 
 * @author liuan
 * @version 1.0,2011-2-24
 */
public class EmptyResponseInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		return super.preHandle(request, response, handler);
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
		Map<String, Object> model = modelAndView.getModel();
		if (model == null || model.isEmpty()){
			ChannelResult channelResult = new ChannelResult();
			channelResult.setSuccess(false);
			channelResult.setMsg("没有数据!");
			modelAndView.addObject(channelResult);
		}
	}
	
	public void afterCompletion(HttpServletRequest request,HttpServletResponse response,
			Object handler,Exception exception){
			Exception ex = (Exception) request.getAttribute(ExceptionHandler.EXCEPTION);
			if(ex != null){
				System.out.println(ex.getMessage());
			}
	}

}
